package com.hist.test;

import com.hist.dao.BlogMapper;
import com.hist.pojo.Blog;
import com.hist.utils.IDUtils;
import com.hist.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @auther Ren Gaoshuai
 * @date 2021-04-04 16:38
 **/
public class MybatisTest1 {


    @Test
    //测试随机生成id
    public void test1(){
        String id = IDUtils.getId();
        System.out.println(id);
    }

    @Test
    public void test2(){
        SqlSession sqlSession = MyBatisUtils.getSession();

        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        Blog blog = new Blog();
        blog.setId(IDUtils.getId());
        blog.setTitle("Mybatis");
        blog.setAuthor("狂神说");
        blog.setCreateTime(new Date());
        blog.setViews(9999);

        mapper.addBook(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("Java");
        mapper.addBook(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("Spring");
        mapper.addBook(blog);

        blog.setId(IDUtils.getId());
        blog.setTitle("微服务");
        mapper.addBook(blog);

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test3(){
        SqlSession sqlSession = MyBatisUtils.getSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap hashMap = new HashMap();
        hashMap.put("title","%a%");
        hashMap.put("author","%说%");

        List<Blog> blogs = mapper.queryBlogIF(hashMap);

        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }


    @Test
    public void test4(){
        SqlSession sqlSession = MyBatisUtils.getSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap hashMap = new HashMap();
        hashMap.put("title","%Java%");
        //hashMap.put("author","%狂%");

        List<Blog> blogs = mapper.queryBlogWhen(hashMap);

        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }
}
